// pages/doctor_side/index/index_history/index_history.js
import $service from '../../../../service';
let idCard = '';
const app = getApp();
let page = 1;
let that;
Page({
  data: {
    wzlslist: [],
    loadingText: '',
    loadingType: true,
    isEsgin: $service.isEsgin,
    idCard: '',
    logId: ''
  },
  onLoad: function (e) {
    page = 1
    wx.setNavigationBarTitle({
      title: e.name + '的病历'
    })
    if (e.type == 'user') {
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#ffffff'
      })
    }
    idCard = e.idCard
    this.setData({
      idCard: e.idCard,
      logId: e.logId + ''
    })
  },
  onShow: function () {
    that = this.data;
    if (!$service.isEsgin) {
      this.getInquiryLogByUserIdList();
    }
  },

  onPullDownRefresh() {
    if ($service.isEsgin) return
    page = 1;
    this.setData({
        loadingText: '加载更多'
    })
    this.getInquiryLogByUserIdList();
    wx.stopPullDownRefresh()
},
onReachBottom: function () {
    if (that.loadingType == true && !$service.isEsgin) {
        page++
        this.getInquiryLogByUserIdList();
    } else {
        this.setData({
            loadingText: '没有更多了'
        })
    }
},
  getInquiryLogByUserIdList() {
    let params = {
      "current": page,
      "identityCard": idCard,
      "size": 10
    }
    app.http.getInquiryLogByUserIdList(params).then(res => {
      let list = res.data.list;
            if (page == 1) {
                that.loadingText = '';
                that.wzlslist = list;
            } else {
                if (list.length <= 0) {
                    that.loadingType = false;
                    that.loadingText = '没有更多了';
                } else {
                    for (var i = 0; i < list.length; i++) {
                        that.wzlslist.push(list[i]);
                        that.loadingText = '加载更多';
                    }
                }
            }
      this.setData({
        wzlslist: that.wzlslist
      })
    }).catch(err => {
      $service.showToast(err.msg);
    })
  },
  wzlsgotodetalis(e) {
    let index = e.currentTarget.dataset.index;
    let num = e.currentTarget.dataset.num;
    let upindex = e.currentTarget.dataset.upindex;
    let id;
    let template;
    let type;
    switch(index) {
      case "1": // 检验检查
        type = 3
        id = that.wzlslist[upindex].inspectionOpinion[num].id;
        break;
      case "2": //健康指导
        type = 2
        id = that.wzlslist[upindex].healthGuidance[num].id;
        break; 
      case "3": //住院建议
        type = 1
        id = that.wzlslist[upindex].hospitalization.inquiryPatientId;
        break;
      case "4": // 处方
        type = 4
        id = that.wzlslist[upindex].inquiryLogPrescription[num].id;
        break;   
      case "5": //申请单
        type = 5
        id = that.wzlslist[upindex].applicationForm[num].id;
        template = that.wzlslist[upindex].applicationForm[num].templateValue
        break;    
    }
    $service.openPage('/pages/user_side/index/index_health_guidance/index_health_guidance?id=' + id + '&value=' + template + "&type=" + type)
  },
  previewImage(e) {
    let upindex = e.currentTarget.dataset.upindex;
    let imgList = []
    imgList = this.data.wzlslist[upindex].mediaMaterials.map(item => item.src)
    imgList.forEach(item => {
      item = item.replace(/https/, 'http')
    })
    wx.previewImage({
      urls: imgList // 需要预览的图片http链接列表
    })
  }
})